package com.shundr.shipper.frame.service;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.widget.EditText;

import com.shundr.shipper.frame.util.LLogger;
import com.shundr.shipper.frame.util.StringUtil;

/**
 * 
 * SmsObserver
 * 
 * @author larry 2015-5-29 下午3:56:07 
 * @version 1.0.0
 *
 */
public class SmsObserver extends ContentObserver {
	public static final String SMS_URI_INBOX = "content://sms/inbox";
	private Activity mActivity = null;
	private EditText verifyText = null;
	
	private Uri SMS_INBOX = Uri.parse("content://sms/");

	public SmsObserver(Activity activity, Handler handler, EditText verifyText) {
		super(handler);
		this.mActivity = activity;
		this.verifyText = verifyText;
	}
	

	@Override
	public void onChange(boolean selfChange) {
		super.onChange(selfChange);
		getSmsFromPhone(mActivity);
	}
	
	/**
	 * 
	 * getSmsFromPhone(这里用一句话描述这个方法的作用)
	 * (这里描述这个方法适用条件 – 可选)
	 * @param activity
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void getSmsFromPhone(Activity activity) {
		try {
			ContentResolver cr = activity.getContentResolver();
			String[] projection = new String[] { "body" };//"_id", "address", "person",, "date", "type
			String where = " date >  " + (System.currentTimeMillis() - 5 * 1000);
			Cursor cur = cr.query(SMS_INBOX, projection, where, null, "date desc");
			if (null == cur)
				return;
			if (cur.moveToNext()) {
				String body = cur.getString(cur.getColumnIndex("body"));
				//这里我是要获取自己短信服务号码中的验证码~~
				LLogger.e("larry", "body : " + body);
				verifyText.setText(StringUtil.patternSmsCode(body));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
